﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.ajax
{
    /// <summary>
    /// GetData 的摘要说明
    /// </summary>
    public class GetData : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";


            string tableName = "";
            string filters = "";
            string orderBy = "";
            List<string> tablePrices = new List<string>();

            if (!string.IsNullOrEmpty(context.Request.QueryString["tablename"]))
            {
                tableName = context.Request.QueryString["tablename"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["filters"]))
            {
                filters = context.Request.QueryString["filters"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["orderby"]))
            {
                orderBy = context.Request.QueryString["orderby"];
            }



            if (!string.IsNullOrEmpty(context.Request.QueryString["pricecolumns"]))
            {
                string[] keywords = context.Request.QueryString["pricecolumns"].Split(new char[] { ',' });
                foreach (string s in keywords)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        tablePrices.Add(s);
                    }
                }
            }
            int getCount = App_Codes.GetCount.Count;
            List<App_Codes.Prices> list = null;

            string _tablePrices = "";
            foreach (string s in tablePrices)
            {
                _tablePrices += s;
            }

            string cacheString = tableName + filters + orderBy + _tablePrices + getCount;
            string jsonString = "";


            object obj = CacheManager.GetCache(cacheString);
            if (obj != null)
            {
                jsonString = obj as string;
            }
            else
            {
                list = TableHtmlGeterate.DataCapture.GetCurrentDatas(tableName, filters, orderBy, tablePrices, getCount);
                jsonString = App_Codes.JsonHelper.ListToJson(list);
                CacheManager.AddCache(cacheString, jsonString, App_Codes.GetCount.CacheTime * 30);
            }


            //list = TableHtmlGeterate.DataCapture.GetCurrentDatas(tableName, filters, orderBy, tablePrices, getCount);
            //jsonString = App_Codes.JsonHelper.ListToJson(list);
            context.Response.Write(jsonString);

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}